/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controller.bean;

import com.entites.Employee;
import com.entites.EventLibrary;
import com.entites.EventRegistration;
import com.model.bean.EmployeeFacadeLocal;
import com.model.bean.EventLibraryFacadeLocal;
import com.model.bean.EventRegistrationFacadeLocal;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author NguyenVan
 */
@ManagedBean
@RequestScoped
public class EventSearchController {

    @EJB
    private EventLibraryFacadeLocal eventLibraryFacade;

    @EJB
    private EventRegistrationFacadeLocal eventRegistrationFacadeLocal;

    @EJB
    private EmployeeFacadeLocal employeeFacadeLocal;
    Login login;
    EventLibrary eventLibrary;
    Employee employee;

    public EventSearchController() {
        eventLibrary = new EventLibrary();
        employee = new Employee();
    }

    private int pages;
    private int currentPages;
    private int noOfRecords;
    private int noOfPage;
    private String parameter;
    private final String DATEFORMAT ="yyyy/MM/dd HH:mm:ss";

    FacesContext fCtx = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(true);

    public HttpSession getSession() {
        return session;
    }

    public void setSession(HttpSession session) {
        this.session = session;
    }
    public String getParameter() {
        return parameter;
    }

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }

    public int getCurrentPages() {
        return currentPages;
    }

    public void setCurrentPages(int currentPages) {
        this.currentPages = currentPages;
    }

    public int getNoOfRecords() {
        return noOfRecords;
    }

    public void setNoOfRecords(int noOfRecords) {
        this.noOfRecords = noOfRecords;
    }

    public int getNoOfPage() {
        return noOfPage;
    }

    public void setNoOfPage(int noOfPage) {
        this.noOfPage = noOfPage;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public List<EventLibrary> getEventLibraryListSearch() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        parameter=(String) session.getAttribute("parameter");
        if (request.getParameter("pages") != null) {
            pages = Integer.parseInt(request.getParameter("pages"));
        }
        login=new Login();
        String username=login.getUsernameSession();
        List<EventLibrary> list;
        int record = 6;
        currentPages = 1;
        if (pages != 0) {
            currentPages = pages;
        }
        int start_row = (currentPages * record) - (record - 1);
        int end_row = start_row + record - 1;
        list = eventLibraryFacade.findEventLibraryByParameter(start_row, end_row, username, parameter);
        noOfRecords = eventLibraryFacade.countRecordSearch(parameter);
        noOfPage = ((int) Math.ceil(noOfRecords * 1.0 / record));

        return list;

    }

    public void redirect(String param) throws IOException{
        session.setAttribute("parameter", param);
        String url="/EventSearch.xhtml";
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        context.redirect(context.getRequestContextPath() + url);
    }
}
